//
//  GNRDataStore.m
//  UUIM
//
//  Created by LvYuan on 2017/4/5.
//  Copyright © 2017年 UUPaotui. All rights reserved.
//

#import "GNRDataStore.h"
#import "TMNetDataInterface.h"

@implementation GNRDataStore

NSString * const kUserName = @"kUserName";
NSString * const kUserPsw = @"kUserPsw";

+ (NSUserDefaults *)userDefaults{
    return [NSUserDefaults standardUserDefaults];
}

+ (void)setObject:(id)obj key:(NSString *)key{
    if (!key) {
        return;
    }
    if (obj) {
        [[self userDefaults] setObject:obj forKey:key];
    }else{
        [[self userDefaults] removeObjectForKey:key];
    }
    [[self userDefaults] synchronize];
}

+ (id)objForKey:(NSString *)key{
    if (!key) {
        return nil;
    }
    return [[self userDefaults]objectForKey:key];
}

+ (instancetype)store{
    static GNRDataStore *c = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        c = [[self alloc] init];
    });
    return c;
}

- (GNRProfileInfoParam *)profileParam{
    if (!_profileParam) {
        _profileParam = [GNRProfileInfoParam new];
    }
    return _profileParam;
}

- (GNRMarkInfoParam *)markInfoParam{
    if (!_markInfoParam) {
        _markInfoParam = [GNRMarkInfoParam new];
    }
    return _markInfoParam;
}

- (instancetype)init{
    if (self = [super init]) {
        _UserName = [GNRDataStore objForKey:kUserName];
        _UserPwd = [GNRDataStore objForKey:kUserPsw];
        self.profileParam.identification = _UserName;
        self.markInfoParam.identification = _UserName;
    }
    return self;
}

+ (void)clear{
    [GNRDataStore store].UserPwd = nil;
    [GNRDataStore store].isLogin = NO;
    [[GNRDataStore store].profileParam clear];
}

- (BOOL)isLogin{
    return _UserName.length&&_UserPwd.length;
}

- (void)setUserName:(NSString *)UserName{
    _UserName = UserName;
    self.profileParam.identification = UserName;
    self.markInfoParam.identification = UserName;
    [GNRDataStore setObject:UserName key:kUserName];
}

- (void)setUserPwd:(NSString *)UserPwd{
    _UserPwd = UserPwd;
    [GNRDataStore setObject:UserPwd key:kUserPsw];
}

//获取个人信息
+ (void)getProfileInfoCompletion:(GNRProfileInfoBlock)completion cache:(BOOL)cache{
    if (cache && [GNRDataStore store].profileParam.code.integerValue==200) {//有缓存
        if (completion) completion([GNRDataStore store].profileParam,YES,nil);
        return;
    }
    [TMNetDataInterface profileInfo:[GNRDataStore store].profileParam update:NO finish:^(BOOL success, TMNetError *error) {
        if (completion) completion([GNRDataStore store].profileParam,[GNRDataStore store].profileParam.code.integerValue==200,error);
    }];
}

//获取个人积分时间等信息
+ (void)getMarkInfoCompletion:(GNRMarkInfoBlock)completion cache:(BOOL)cache{
    if (cache && [GNRDataStore store].markInfoParam.code.integerValue==200) {//有缓存
        if (completion) completion([GNRDataStore store].markInfoParam,YES,nil);
        return;
    }
    [TMNetDataInterface getMarkInfo:[GNRDataStore store].markInfoParam finish:^(BOOL success, TMNetError *error) {
        if (completion) completion([GNRDataStore store].markInfoParam,[GNRDataStore store].profileParam.code.integerValue==200,error);
    }];
}

@end
